package org.oexam.ostudy.facade;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import org.oexam.ostudy.facade.struct.AddAndViewTestsDisplayDetails;
import org.oexam.ostudy.facade.struct.InsertNewTestDetails;
import org.oexam.ostudy.facade.struct.InsertNewTestSetDetails;
import org.oexam.ostudy.facade.struct.TeacherList;
import org.oexam.ostudy.facade.struct.TeacherNameAndID;
import org.oexam.ostudy.facade.struct.TestNameAndId;
import org.oexam.ostudy.facade.struct.TestSetNameList;
import org.oexam.ostudy.sl.ManageTeacherList;
import org.oexam.ostudy.sl.ManageTestSets;
import org.oexam.ostudy.struct.Teachers;

public class TestSetPages {
	
	 public String addGivenTestSet(InsertNewTestSetDetails details) throws SQLException, NamingException{
         if(details.noOfTests==0||details.testSet_name.equals("")||details.testSetMonth.equals("")||details.testSetYear==0){
                 
                 return "Please enter all the fields. All fields on this page are mandatory.";
                 
         }
         
         if(details.testSetMonth.length()>3){
                 
                 return "Please enter only the first three letters of the month.";
         }
         
         return new ManageTestSets().addTestSet(details);
         
 
         
 }
	 
	 public String addGivenTestToTestSet(InsertNewTestDetails details) throws SQLException, NamingException{
         if((details.getNo_of_questions()==0)||
        		 details.getTeacher()==0||
        		 details.getTest_day()==0||
        		 details.getTest_duration()==0||
        		 details.getTest_month()==0||
        		 details.getTest_name()==null||
        		 details.getTest_name().equals("")||
        		 details.getTest_year()==0){
                 
                 return "Please enter all the fields. All fields on this page are mandatory.";
                 
         }
         
         if(details.getTest_month()>12){
                 
                 return "Please a valid month.";
         }
         
         if(details.getTest_day()>31){
             
             return "Please a valid date.";
     }
         
         
         return new ManageTestSets().addTesttoTestSet(details);
         
 
         
 }
	 
	 public TestSetNameList getAllTestSetnames() throws SQLException, NamingException{
		 
		 return new ManageTestSets().getAllTestSetsName();
	 }

	 public AddAndViewTestsDisplayDetails getAllViewAndAddTestDisplayDetails(int testSetID) throws SQLException, NamingException{
		 
		 AddAndViewTestsDisplayDetails addAndViewTestsDisplayDetails = new AddAndViewTestsDisplayDetails();
		 
		 addAndViewTestsDisplayDetails.setTestnames(new ManageTestSets().getTestsNameAndIdForATestSet(testSetID));
		 
		TeacherList teacherList = new ManageTeacherList().getTeacherList();
		
		List<TeacherNameAndID> teacherNameAndIDs = new ArrayList<TeacherNameAndID>();
		for(Teachers teacher : teacherList.getTeacherList()){
			TeacherNameAndID nameAndID = new TeacherNameAndID();
			nameAndID.setTeacherID(teacher.getTeacher_id());
			nameAndID.setTeacherName(teacher.getFirst_name()+" "+teacher.getLast_name());
			teacherNameAndIDs.add(nameAndID);
			addAndViewTestsDisplayDetails.setTeachernames(teacherNameAndIDs);
		}
		 
		addAndViewTestsDisplayDetails.setTeachernames(teacherNameAndIDs);
		
	
		addAndViewTestsDisplayDetails.setTestSetName(new ManageTestSets().getTestSetNameUsingId(testSetID));
		
		return addAndViewTestsDisplayDetails;
		
	 }
	 
	 public String deleteTest(int testID) throws SQLException, NamingException{
        
         // TODO Pradeep
 
         return "delete failed";
 }
	 
	 public String deleteTestSet(int testID) throws SQLException, NamingException{
	        
         // TODO Pradeep
		 
         return "delete failed";
 }

}
